package com.biz.ssm.controller;

import com.alibaba.fastjson.JSON;
import com.biz.ssm.base.Response;
import com.biz.ssm.enums.CodeEnum;
import com.biz.ssm.security.MyUserDetails;
import com.biz.ssm.security.SecurityContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class IndexController {

    @Value("${spring.application.name}")
    private String name;

    @GetMapping("/")
    public String home() {
        return name + "~" + System.currentTimeMillis();
    }

    @GetMapping("/notLogin")
    public Response notLogin() {
        return Response.resp(CodeEnum.NOT_LOGIN);
    }

    @GetMapping("/index")
    @PreAuthorize("isAuthenticated()")
    public MyUserDetails index() {
        log.info("index......................" + JSON.toJSONString(SecurityContextUtils.getCurrentUser()));
        return SecurityContextUtils.getCurrentUser();
    }
}
